package com.zy.controller;

import com.zy.pojo.Role;
import com.zy.service.RoleService;
import com.zy.vo.ResultVo;
import com.zy.vo.RoleVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
public class RoleController {
    @Resource
    private RoleService roleService;

    @GetMapping("/rolelist/{pageNo}/{pageSize}")
    @RequiresPermissions("role:list")
    public ResultVo getrolelist(@PathVariable Integer pageNo,@PathVariable Integer pageSize, RoleVo roleVo){
        return ResultVo.sucess(roleService.getPageRoleList(pageNo,pageSize,roleVo));
    }

    //添加
    @PostMapping("/roleadd")
    @RequiresPermissions("role:add")
    public ResultVo add(@RequestBody Role role){
        try {
            role.setBirthday(new Date());
            roleService.add(role);
            return ResultVo.sucess("角色添加成功");
        }catch (Exception e){
            return ResultVo.fail("角色添加失败",e);  //包含了打印错误日志
        }
    }

    //删除
    @GetMapping("/roledel")
    @RequiresPermissions("role:del")
    public ResultVo delete(String id){
        try {
          roleService.delete(Integer.parseInt(id));
            return ResultVo.sucess("角色删除成功");
        }catch (Exception e){
            return ResultVo.fail("角色删除失败",e);  //包含了打印错误日志
        }
    }

    //修改
    @PostMapping("/roleupdate")
    @RequiresPermissions("role:update")
    public ResultVo update(@RequestBody Role role){
        try {
            roleService.update(role);
            return ResultVo.sucess("角色更新成功");
        }catch (Exception e){
            return ResultVo.fail("角色更新失败",e);  //包含了打印错误日志
        }
    }

}
